package com.yc.thread.lock;

import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;

public class TestExchanger {
	
	static Exchanger<String> s = new Exchanger<String>();

	public static void main(String[] args) {
		//T1首先赶到约会地
		new Thread(() -> {
			String gift = "T1的东西";
			try {
				gift = s.exchange(gift);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("T1：我现在有" + gift);
		}, "T1").start();
		
		//T1正在焦急的等待中
		try {
			TimeUnit.SECONDS.sleep(2);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		
		//T2姗姗来迟
		new Thread(() -> {
			String gift = "T2的东西";
			try {
				gift = s.exchange(gift);//两人交换了礼物后，各自分道扬镳
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("T2：我现在有" + gift);
		}, "T2").start();
		
		

	}

}
